package zucc.edu.cn.chengchengparttime.My;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import zucc.edu.cn.chengchengparttime.LoginAndReg.LoginActivity;
import zucc.edu.cn.chengchengparttime.R;
import zucc.edu.cn.chengchengparttime.dbHomeHelper;

public class MyAppSetting_AccountSecurity extends AppCompatActivity implements View.OnClickListener{
    private Context context;
    private List<String> data=new ArrayList<>(); //数据列表
    private TextView modifyphone;
    private TextView modifypwd;
    private TextView cancelaccount;
    private ImageView goback;
    private TextView modifyphone_text;
    private String pwd=null,pwd2=null,newphone=null;
    private boolean match=true,match2=true;
    dbHomeHelper homeHelper; //数据库帮助类
    private SQLiteDatabase mDatabase;// 数据库实例
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_my_appsetting_account);
        context = this.getBaseContext();
        initView();
        mDatabase=SQLiteDatabase.openDatabase("/data/user/0/zucc.edu.cn.chengchengparttime/databases/Chengcheng.db",null,SQLiteDatabase.OPEN_READWRITE);
        Cursor c=mDatabase.query("tbl_user", null, "UserID=?", new String[]{LoginActivity.getCurrentUser()}, null, null,null);
        c.moveToFirst();//游标移到第一条记录
        // 打印记录
        String[] x2 = new String[30];
        while(!c.isAfterLast()){
            String rowResults = "||";
            for (int i = 0; i < c.getColumnCount(); i++) {
                rowResults = rowResults.concat(c.getString(i) + "||");
                x2[i]=c.getString(i);
            }
            c.moveToNext();
        }
        c.close();//关闭游标工厂

        String nub=x2[6];
        modifyphone_text.setText(nub.substring(0,3)+"****"+nub.substring(7));
    }

    private void initView() {
        //初始化控件
        modifyphone=findViewById(R.id.modifyphone);
        modifypwd=findViewById(R.id.modifypwd);
        cancelaccount=findViewById(R.id.cancelaccount);
        goback=findViewById(R.id.goback);
        modifyphone_text=findViewById(R.id.modifyphone_text);
        //设置点击事件监听器
        modifyphone.setOnClickListener(this);
        modifypwd.setOnClickListener(this);
        cancelaccount.setOnClickListener(this);
        goback.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.modifyphone: //注册
                v=getLayoutInflater().inflate(R.layout.dialog_modify_phone,null);
                final EditText modify_newphone=(EditText)v.findViewById(R.id.modify_newphone);
                final TextView remind1=v.findViewById(R.id.remind);

                new AlertDialog.Builder(this).setTitle("修改密码")
                        .setIcon(null)
                        .setView(v)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                newphone=(modify_newphone.getText().toString());
                                if(newphone.length()==11){
                                    dbHomeHelper.update_phone(mDatabase,newphone);
                                }else{
                                    match2=false;
                                }
                            }
                        }).setNegativeButton("取消",null).show();
//                if(match==false){
//                    Toast.makeText(this, "请确认两次输入相同的密码" , Toast.LENGTH_SHORT).show();
//                }
//                if(match2==false){
//                    Toast.makeText(this, "输入的密码为空" , Toast.LENGTH_SHORT).show();
//                }
                break;
            case R.id.modifypwd:
                v=getLayoutInflater().inflate(R.layout.dialog_modify_pwd,null);
                final EditText modify_newpwd=(EditText)v.findViewById(R.id.modify_newpwd);
                final EditText modify_newpwd2=(EditText)v.findViewById(R.id.modify_newpwd2);
                final TextView remind=v.findViewById(R.id.remind);

                new AlertDialog.Builder(this).setTitle("修改密码")
                        .setIcon(null)
                        .setView(v)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                pwd=(modify_newpwd.getText().toString());
                                pwd2=(modify_newpwd2.getText().toString());
                                if(pwd!=pwd2){
                                    match=false;
                                }else if(pwd.length()==0){
                                    match2=false;
                                }else{
                                    dbHomeHelper.update_pwd(mDatabase,pwd);
                                }
                            }
                        }).setNegativeButton("取消",null).show();
//                if(match==false){
//                    Toast.makeText(this, "请确认两次输入相同的密码" , Toast.LENGTH_SHORT).show();
//                }
//                if(match2==false){
//                    Toast.makeText(this, "输入的密码为空" , Toast.LENGTH_SHORT).show();
//                }
                break;
            case R.id.cancelaccount:

                break;
            //登录验证
            case R.id.goback:
                Toast.makeText(this.context, "点击了返回" , Toast.LENGTH_SHORT).show();
                //跳转到APP设置界面
                Intent intent2 = new Intent(MyAppSetting_AccountSecurity.this, MyAppSetting.class);
                startActivity(intent2);
                finish();
                break;
        }
    }
}
